﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DONET101_Types
{
    class Program
    {
        static void HexDecimal()
        {
            string input = "Hello World!";
            char[] values = input.ToCharArray();
            foreach (char c in values)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(c);
                // Convert the decimal value to a hexadecimal value in string form.
                string hex = String.Format("{0:X}", value);
                Console.WriteLine("Hexadecimal value of {0} is {1}", c, hex);
            }
        }

        static void TestBitConverter()
        {
            byte[] bytes = { 0, 0, 0, 25 };

            // If the system architecture is little-endian (that is, little end first),
            // reverse the byte array.
            if (BitConverter.IsLittleEndian)
                Array.Reverse(bytes);

            int i = BitConverter.ToInt32(bytes, 0);
            Console.WriteLine("int: {0}", i);

            bytes = BitConverter.GetBytes(201805978);
            Console.WriteLine("byte array: " + BitConverter.ToString(bytes));
        }

        static void TestConverter()
        {
            int i = Convert.ToInt32("29");
            i++;
            Console.WriteLine(i);

            i = Int32.Parse("-105");
            Console.WriteLine(i);

            int j;
            Int32.TryParse("-105", out j);
            Console.WriteLine(j);

            try
            {
                int m = Int32.Parse("abc");
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }

            string s = "abc";
            int k;
            bool parsed = Int32.TryParse(s, out k);

            if (!parsed)
                Console.WriteLine("Int32.TryParse could not parse '{0}' to an int.\n", s);
        }

        static void TestCasting()
        {
            int i = 10;
            float f = 0;
            f = i;  // An implicit conversion, no data will be lost.
            f = 0.5F;
            i = (int)f;  // An explicit conversion. Information will be lost.
        }

        static void Main(string[] args)
        {

            int i = 123;
            object o = (object)i;  // boxing

            o = 123;
            i = (int)o;  // unboxing

            i = 123;
            o = (object)i;  // explicit boxing
        }
    }

    class TestUnboxing
    {
        static void ExceptionTest()
        {
            int i = 123;
            object o = i;  // implicit boxing

            try
            {
                int j = (short)o;  // attempt to unbox

                System.Console.WriteLine("Unboxing OK.");
            }
            catch (System.InvalidCastException e)
            {
                System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
            }
        }
    }
}
